package com.yangaobo.mapper;

import com.yangaobo.dto.RolePageDTO;
import com.yangaobo.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 杨奥博
 */
@Repository
public interface RoleMapper {
    @Insert("""
            insert into ums_role(`title`, `info`, `version`, `deleted`, `created`, `updated`)
            values (#{title}, #{info}, #{version}, #{deleted}, #{created}, #{updated})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Role role);

    @Select("""
            select * from ums_role t
            where t.id = #{param} and t.deleted = 0
            """)
    Role select(Long id);

    @Select("""
            <script>
            select * from ums_role t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Role> list(RolePageDTO dto);

    @Update("""
            <script>
            update ums_role
            <set>
                <if test='title != null'> title = #{title}, </if>
                <if test='info != null'> info = #{info}, </if>
                <if test='deleted != null'> deleted = #{deleted}, </if>
                <if test='created != null'> created = #{created}, </if>
                <if test='updated != null'> updated = #{updated}, </if>
                version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(Role role);

    @Update("""
            update ums_role set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_role set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Select("""
            select * from ums_role t1
            where t1.id in
            (select t2.fk_role_id from ums_emp_role t2
            where t2.fk_emp_id = #{empId} and t2.deleted = 0) and t1.deleted = 0
            """)
    List<Role> listByEmpId(Long empId);

    @Update("""
        update ums_emp_role t1 set deleted = 1, updated = current_timestamp where t1.fk_emp_id = #{empId}
        """)
    int deleteByEmpId(Long empId);

    @Insert("""
            <script>
            insert into ums_emp_role(fk_emp_id, fk_role_id, version, deleted, created, updated) values 
            <foreach collection='ids' item='e' separator=','>
            (#{empId}, #{e}, 0, 0, current_timestamp, current_timestamp)
            </foreach>
            </script>
            """)
    int updateByEmpId(@Param("empId") Long empId, @Param("ids") List<Long> ids);

}
